﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic;
using LotteryVote.Entity;
using LotteryVote.Utility;
using LotteryVote.Logic.Web;

namespace LotteryVote
{
    public partial class EditCommission : CompanyPage
    {
        int groupId;
        CommisionGroupFacade commGroupFacade = new CommisionGroupFacade();
        GamePlayWayFacade gpwFacade = new GamePlayWayFacade();
        CompanyTypeFacade comTypeFacade = new CompanyTypeFacade();
        public CommissionGroupModel GroupModel { get; set; }
        public IList<EnumDescriptionAttribute> CompanyTypeList;
        public IList<EnumDescriptionAttribute> GameTypeList;
        protected void Page_Load(object sender, EventArgs e)
        {
            Execute(() =>
            {
                InitParams();
            });
        }

        void InitParams()
        {
            if (string.IsNullOrEmpty(Request["GroupId"]))
                Response.Redirect("~/Default.aspx");
            groupId = int.Parse(Request["GroupId"]);
            GroupModel = commGroupFacade.GetCommissionGroupModel(groupId);
            GameTypeList = EnumHelper.GetDescription<GameTypeEnum>();
            CompanyTypeList = EnumHelper.GetDescription<CompanyType>();
        }

        public ConcreteCommission GetComm(int comTypeId, int gameTypeId)
        {
            return GroupModel.ConcreteCommissionList.Where(it => it.CompanyType.Id == comTypeId && it.GameType.GameId == gameTypeId).FirstOrDefault() ?? new ConcreteCommission();
        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            Execute(() =>
            {
                var items = JSONHelper.ToObject<List<CommissionItem>>(hd_commission.Value);
                commGroupFacade.UpdateCommissionGroup(groupId, items);
                GroupModel = commGroupFacade.GetCommissionGroupModel(groupId);
            });
        }
    }
}